package com.api.service.v1;

import com.admin.entity.Apk;
import com.admin.mapper.ApkMapper;
import com.api.bean.ApkBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.ResponseBean;
import com.util.MD5Util;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@Service
public class ApkAPIService {

    @Autowired
    private ApkMapper apkDao;

    @Value("${upload.dir}")
    private String upload;

    public ResponseBean findapk() throws IOException {

        //String url = SERVER;// "http://192.168.2.156:8080/wo-web";

        String ROOT_PATH_NEW = upload;
        QueryWrapper q = new QueryWrapper();
        q.eq("apk_is_it_public", "公开");
        List<Apk> apks = apkDao.selectList(q);
        List<ApkBean> list = new ArrayList<ApkBean>();
        if (apks == null || apks.size() == 0) {

        } else {
            for (Apk apk : apks) {
                ApkBean apkBean = new ApkBean();
                String name = apk.getApkName();
                if (StringUtil.isNotNull(name))
                    apkBean.setName("");
                else
                    apkBean.setName("");

                if (StringUtil.isNotNull(name))
                    apkBean.setApkName(name);
                else
                    apkBean.setApkName("");

                if (StringUtil.isNull(apk.getApkPictureUrl())) {
                    apkBean.setImage(null);
                } else {
                    String md5 = MD5Util.md5sum(ROOT_PATH_NEW + apk.getApkPictureUrl().substring(7));
                    apkBean.setImage(apk.getApkPictureUrl() + "?md5=" + md5);
                }

                apkBean.setPackageName(apk.getApkPackage());

                if (StringUtil.isNull(apk.getApkUrl())) {
                    apkBean.setPackageUrl(null);
                } else {
                    String md5 = MD5Util.md5sum(ROOT_PATH_NEW + apk.getApkUrl().substring(7));
                    apkBean.setPackageUrl(apk.getApkUrl() + "?md5=" + md5);
                }
                list.add(apkBean);
            }
        }
        return new ResponseBean(200, "成功", list);
    }

    /**
     * apk使用+1
     *
     * @param packageName
     * @return
     */
    public int tongji_add(String packageName) {
        Map map = new HashMap();
        map.put("apk_package", packageName);
        List<Apk> list = apkDao.selectByMap(map);
        if (list.size()==0){
            return 0;
        }
        for (Apk apk : list) {
            Integer oldSum = apk.getApkStatistics();
            if (oldSum == null) {
                oldSum = 0;
            }
            apk.setApkStatistics(oldSum + 1);
            apkDao.updateById(apk);
            return oldSum+1;
        }
        return 1;
    }

}
